package gao.xiaolei.dao;

import gao.xiaolei.entity.UserRole;
import gao.xiaolei.entity.UserRoleId;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("UserRoleDao")
public interface UserRoleDao extends JpaRepository<UserRole, UserRoleId> {

    @Query("select id.roleId from UserRole where id.userId=:userId")
    public List<String> getRoleByUserId(@Param("userId") String userId);

    @Query("select id.userId from UserRole where id.roleId in:roleIds")
    public List<String> getUserByRoleIdIn(@Param("roleIds") List<String> roleIds);

    @Query("select id.userId from UserRole where id.roleId=:roleId")
    public List<String> getUserByRoleId(@Param("roleId") String roleId);

    @Modifying
    @Query("delete from UserRole where id.userId=:userId")
    public void deleteAllByUserId(@Param("userId") String userId);
}
